package com.zx.idc.backend.gui.api.watch.handler;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.api.ftp.Stat;
import com.zx.idc.backend.gui.api.watch.handler.base.AbstractFtpUploadResultHandler;
import com.zx.idc.backend.gui.api.watch.pojo.FtpUploadResult;
import com.zx.idc.common.lang.ListUtil;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.slog.entity.SlogHotDomain;
import com.zx.idc.ds.slog.entity.SlogHotIp;
import com.zx.idc.ds.slog.service.ISlogHotDomainService;
import com.zx.idc.ds.slog.service.ISlogHotIpService;
import com.zx.idc.ds.slog.service.ISlogHotUrlService;
import com.zx.idc.xml.entity.activeresources.ActiveResources;
import com.zx.idc.xml.entity.activeresources.finddomainlist.FindDomainList;
import com.zx.idc.xml.entity.activeresources.findiplist.FindIpList;
import com.zx.idc.xml.entity.activeresources.findurllist.FindUrlList;
import com.zx.idc.xml.utils.JaxbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.zx.idc.common.attributes.FTPReportDataTypeAttributes.FTP_ACTIVE_RESOURCE_MONITORING_RECORD;

/**
 * 活跃资源监测记录结果处理器
 */
@Component
//@Transactional(rollbackFor = Exception.class) //不需要声明事务,当前的类相当于controller
public class ActiveResourceMonitorRecordResultHandler extends AbstractFtpUploadResultHandler {

    @Autowired
    private ISlogHotDomainService iSlogHotDomainService;

    @Autowired
    private ISlogHotIpService iSlogHotIpService;

    @Autowired
    private ISlogHotUrlService iSlogHotUrlService;


    /**
     * 匹配
     *
     * @param type
     * @return
     */
    @Override
    public boolean adapter(String type) {
        return FTP_ACTIVE_RESOURCE_MONITORING_RECORD.equals(type);
    }

    /**
     * 上报成功回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadSuccess(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("active resource monitoring record result success processor start!");
        updateStatusIsReportingTemplate(
            stat,
            ftpUploadResult,
            filePath,
            ReportStatusConstant.REPORTED,
            ReportStatusConstant.REPORTED,
            String.valueOf(ReportStatusConstant.REPORTED)
        );
        log.debug("active resource monitoring record result success processor done!");
    }

    /**
     * 上报异常回调,需要处理结果则重写改方法
     *
     * @param stat            ftp扫描的原信息对象
     * @param ftpUploadResult 解析后的ftp上报结果对象
     * @param filePath        上报源文件的全路径
     */
    @Override
    public void uploadFailed(Stat stat, FtpUploadResult ftpUploadResult, String filePath) {
        log.debug("active resource monitoring record result failed processor start!");
        updateStatusIsReportingTemplate(
            stat,
            ftpUploadResult,
            filePath,
            ReportStatusConstant.REPORTING_EXCEPTION,
            ReportStatusConstant.REPORTING_EXCEPTION,
            String.valueOf(ReportStatusConstant.REPORTING_EXCEPTION)
        );
        log.debug("active resource monitoring record result failed processor done!");
    }


    /**
     * 更新上报中的数据模板
     *
     * @param stat               ftp扫描对象
     * @param domainReportStatus 域名上报状态
     * @param ipReportStatus     ip上报状态
     * @param urlReportStatus    url上报状态
     */
    private void updateStatusIsReportingTemplate(Stat stat, FtpUploadResult ftpUploadResult, String filePath, Integer domainReportStatus, Integer ipReportStatus, String urlReportStatus) {
        //读出数据 解析数据 修改状态
        ActiveResources activeResources = JaxbUtils.xmlFileToBean(filePath, ActiveResources.class);
        if (activeResources == null) {
            return;
        }
        //修改上报状态
        //域名
        List<FindDomainList> findDomainList = activeResources.getFindDomainList();
        if (ListUtil.isNotEmpty(findDomainList)) {

            findDomainList.forEach(obj ->
                iSlogHotDomainService.update(
                    new SlogHotDomain()
                        .setReportStatus(domainReportStatus),
                    new EntityWrapper<>(
                        new SlogHotDomain()
                            .setFindDomainListDomain(obj.getFindDomainListDomain())
                            .setReportStatus(ReportStatusConstant.REPORTING)
                    )
                )
            );
        }

        //ip
        List<FindIpList> findIpList = activeResources.getFindIpList();
        if (ListUtil.isNotEmpty(findIpList)) {
            findIpList.forEach(obj ->
                iSlogHotIpService.update(
                    new SlogHotIp()
                        .setIsInIpSeg(ipReportStatus),
                    new EntityWrapper<>(
                        new SlogHotIp()
                            .setFindIpListIp(obj.getFindIpListIp())
                            .setIsInIpSeg(ReportStatusConstant.REPORTING)
                    )
                )
            );
        }

    }
}
